extern crate pool_thread;
use pool_thread::pool_thread::PoolThread;

use std::net::{TcpListener, TcpStream};
use std::io::{Read, Write};
use std::fs::File;

fn main() {
    let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
    let pt = PoolThread::new(4);
    for stream in listener.incoming().take(2) {
        let _stream = stream.unwrap();
        pt.execute(|| {
            handler_connection(_stream);
        });
    }
    println!("Shutting down.");
}

fn handler_connection(mut stream: TcpStream) {
    let mut buffer = [0;1024];
    stream.read(&mut buffer).unwrap();
    println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
    let mut contents = String::new();
    let mut file = File::open("hello.html").unwrap();
    file.read_to_string(&mut contents).unwrap();
    let response = format!("HTTP/1.1 200 OK\r\n\r\n{}", contents);
    stream.write(response.as_bytes()).unwrap();
    stream.flush().unwrap();
}
